package com.xyd.Servlet;

import com.xyd.Biz.Impl.ProductClassBizImpl;
import com.xyd.Biz.ProductClassBiz;
import com.xyd.Entity.Commodity;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SelCommodityServlet")
public class SelCommodityServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProductClassBiz productClassBiz = new ProductClassBizImpl();
        int common_id = Integer.parseInt(req.getParameter("common_id"));
        int pagenum = Integer.parseInt(req.getParameter("pagenum"));

        int max = productClassBiz.countgetCommodityByCommonId(common_id);
        int pagemax = max%12==0?max/12:max/12+1;
        pagenum = pagenum<1?1:pagenum;
        pagenum = pagenum>pagemax?pagemax:pagenum;

        List<Commodity> list = productClassBiz.getCommodityByCommonId(common_id,(pagenum-1)*12);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pagenum",pagenum);
        JsonServlet.getJson(req,resp,map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
